//express服务器入口文件
const express = require('express')
const { getStaticData, writeDocument, getDocument, getTopicHome, writeYiriDoc, getNews } = require('./mongoose')
const { main, weChatDataParse } = require("./puppteer")
const app = express()

app.use(express.json()) // for parsing application/json
app.use(express.urlencoded({ extended: true }))
const port = 3000

app.get('/', (req, res) => {
    res.send('Hello World!')
})

app.get('/api/weichat', async (req, res) => {
    const { uri, date } = req.query
    console.log(date)
    res.header("Content-Type", "application/json; charset=utf-8")
    res.header("Access-Control-Allow-Origin", "*");
    const data = await main(uri)
    const result = await weChatDataParse(data, date)

    res.json(result)
})


//获取新闻列表
app.get('/news', (req, res) => {
    const query = {
        condition: req.query.condition || {},
        limit: parseInt(req.query.limit),
        skip: parseInt(req.query.skip)
    }
    getNews(query).then(data => {
        res.json(data)
    })
})


//写一日文档
app.post('/yiriDoc', (req, res) => {
    writeYiriDoc(req.body).then(data => res.json(data))
})

//获取文档
app.get('/getDoc', (req, res) => {
    const query = { ...req.query }
    try { query.condition = JSON.parse(query.condition) } catch (err) { query.condition = {} }
    query.limit = parseInt(query.limit)
    query.skip = parseInt(query.skip)
    getDocument(query).then(data => res.json(data))
})

//新增文档
app.post('/submit', (req, res) => {
    console.log(req.body)
    writeDocument(req.body).then(data => res.json(data))
})

//页面静态数据
app.get('/api/staticData', (req, res) => {
    getStaticData({}).then(data => res.json(data))
})

//搜索文档
app.get('/api/queryDocument', async (req, res) => {
    if (req.query.content) {
        const dataList = []
        const query1 = {
            condition: {
                title: {
                    $regex: req.query.content
                }
            },
            limit: 5,
            skip: 0
        }
        const query2 = {
            condition: {
                content: {
                    $regex: req.query.content
                }

            },
            limit: 5,
            skip: 0
        }
        const data1 = await getDocument(query1)
        const data2 = await getDocument(query2)
        dataList.push(...data1.res)
        dataList.push(...data2.res)
        let newList = []
        dataList.forEach(v => {
            let flag = false
            newList.forEach(i => {
                if (v.id === i.id) {
                    flag = true
                }
            })
            if (!flag) {
                if (newList.length < 4) {

                    newList.push(v)
                }
            }
        })
        res.json(newList)

    }
})

//获取热门文档
app.post('/api/getHot', async (req, res) => {
    const data = await getDocument(req.body)
    res.json(data.res)
})

//获取目录 
app.get('/api/context', (req, res) => {
    const query = {
        condition: { classify: req.query.topic },
        limit: 999,
        skip: 0
    }
    // console.log('ok')
    getDocument(query).then(data => {
        const document = data.res
        const [item] = document
        const { classify } = item
        const context = [
            {
                name: (document.filter(v => v.chapter === '常识篇'))[0].chapter,
                titles: (document.filter(v => v.chapter === '常识篇')).map(v => v.title),
                to: (document.filter(v => v.chapter === '常识篇')).map(v => v.to),
                id: (document.filter(v => v.chapter === '常识篇')).map(v => v.id)
            },
            {
                name: (document.filter(v => v.chapter === '病因篇'))[0].chapter,
                titles: (document.filter(v => v.chapter === '病因篇')).map(v => v.title),
                to: (document.filter(v => v.chapter === '病因篇')).map(v => v.to),
                id: (document.filter(v => v.chapter === '病因篇')).map(v => v.id)
            },
            {
                name: (document.filter(v => v.chapter === '症状篇'))[0].chapter,
                titles: (document.filter(v => v.chapter === '症状篇')).map(v => v.title),
                to: (document.filter(v => v.chapter === '症状篇')).map(v => v.to),
                id: (document.filter(v => v.chapter === '症状篇')).map(v => v.id)
            },
            {
                name: (document.filter(v => v.chapter === '诊断篇'))[0].chapter,
                titles: (document.filter(v => v.chapter === '诊断篇')).map(v => v.title),
                to: (document.filter(v => v.chapter === '诊断篇')).map(v => v.to),
                id: (document.filter(v => v.chapter === '诊断篇')).map(v => v.id)
            },
            {
                name: (document.filter(v => v.chapter === '治疗篇'))[0].chapter,
                titles: (document.filter(v => v.chapter === '治疗篇')).map(v => v.title),
                to: (document.filter(v => v.chapter === '治疗篇')).map(v => v.to),
                id: (document.filter(v => v.chapter === '治疗篇')).map(v => v.id)
            },
            {
                name: (document.filter(v => v.chapter === '预防保健篇'))[0].chapter,
                titles: (document.filter(v => v.chapter === '预防保健篇')).map(v => v.title),
                to: (document.filter(v => v.chapter === '预防保健篇')).map(v => v.to),
                id: (document.filter(v => v.chapter === '预防保健篇')).map(v => v.id)
            }
        ]
        res.json({ context, classify })
    })
})

//文档页数据
app.get('/document/:id', (req, res) => {
    console.log(req.params)
    getTopicHome(req.params.id).then(data => res.json(data))
})
app.get('/document/:topic/:id', (req, res) => {
    console.log(req.params)
    const query = {
        condition: {
            id: req.params.id
        },
        limit: 999,
        skip: 0
    }
    getDocument(query).then(data => res.json(data))
})

app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`)
})